<?php

/**
* 微博通插件
*/

add_action('admin_menu', 'wbto_menu');
function wbto_menu() {
  add_options_page('微博通同步设置', '微博通同步', 8, __FILE__, 'wbto_options');
}
function wbto_options() {
	echo '<div class="wrap">';
	echo '<h2>微博通同步</h2>';

	echo '<form method="post" action="options.php">';
	echo wp_nonce_field('update-options');

	echo '<table class="form-table">';

	echo '<tr valign="top">';
	echo '<th scope="row">用户名 <a href="http://www.wbto.cn/?app=wp">注册</a></th>';
	echo '<td><input type="text" name="wbto_username" value="'.get_option('wbto_username').'" /></td>';
	echo '</tr>';
	 
	echo '<tr valign="top">';
	echo '<th scope="row">密码</th>';
	echo '<td><input type="password" name="wbto_password" value="'.get_option('wbto_password').'" /></td>';
	echo '</tr>';

	echo '</table>';

	echo '<input type="hidden" name="action" value="update" />';
	echo '<input type="hidden" name="page_options" value="wbto_username,wbto_password" />';

	echo '<p class="submit">';
	echo '<input type="submit" name="submit" id="submit" class="button-primary" value="保存更改" />';
	echo '</p>';

	echo '</form>';
	echo '</div>';
}

function send_to_wbto($post_ID) {
	$username = get_option('wbto_username');
	$password = get_option('wbto_password');
	$posted = get_post($post_ID);
	
	$fields = array();
	$excerpt = stripslashes(get_post_meta($post_ID,'weibo',true));
	if(!$excerpt)$excerpt = $posted->post_excerpt;
	if(!$excerpt)$excerpt = stripslashes(get_post_meta($post_ID,'description',true));
	if(!$excerpt)$excerpt = mb_strimwidth(strip_tags($posted->post_content),0,150,'...');
	$excerpt = trim(strip_tags($excerpt));
	$url = $posted->guid;
	if(!$url)$url=get_bloginfo('url').'/?p='.$post_ID;
	$fields['source'] = 'wordpress';
	//$fields['content'] = urlencode('【'.$posted->post_title.$url.'】'.mb_strimwidth(strip_tags($excerpt),0,150,'...'));
	//$fields['content'] = urlencode(mb_strimwidth(strip_tags(trim($posted->post_title.'@'.get_the_author_meta('display_name',$posted->post_author).' ^_^ '.$excerpt)),0,200,' ').$url);
	$fields['content'] = urlencode('【'.$posted->post_title.'】@红幛 '.$excerpt.$url);
	update_post_meta($post_ID,'weibo',$excerpt) or add_post_meta($post_ID,'weibo',$excerpt,true);
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, "http://wbto.cn/api/update.json");
	curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
	curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE);
	curl_setopt($ch, CURLOPT_TIMEOUT, 10);
	curl_setopt($ch, CURLOPT_POST, TRUE);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
	$result = curl_exec($ch);
	curl_close($ch);  
}
function if_send_weibo($post_ID){
	$weibo = get_post_meta($post_ID,'weibo',true);
	$posted = get_post($post_ID);
	$excerpt = stripslashes(get_post_meta($post_ID,'weibo',true));
	if(!$excerpt)$excerpt = $posted->post_excerpt;
	if(!$excerpt)$excerpt = stripslashes(get_post_meta($post_ID,'description',true));
	if(!$excerpt)$excerpt = mb_strimwidth(strip_tags($posted->post_content),0,150,'...');
	$excerpt = trim(strip_tags($excerpt));
	if($weibo == $excerpt) return;
	send_to_wbto($post_ID);
	update_post_meta($post_ID,'weibo',$excerpt) or add_post_meta($post_ID,'weibo',$excerpt,true);
}
add_action('publish_post', 'if_send_weibo');
